<?php

namespace app\controllers\merchant\shop;

use app\models\merchant\system\OperationRecordModel;
use Yii;
use yii\web\MerchantController;
use app\models\merchant\system\BargainModel;
use app\models\merchant\app\AppAccessModel;

class BargainController extends MerchantController{

    public $enableCsrfValidation = false; //禁用CSRF令牌验证，可以在基类中设置

    public function actionList(){
        if (yii::$app->request->isGet) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->get(); //获取地址栏参数

            //设置类目 参数
            $must = ['key'];
            $rs = $this->checkInput($must, $params);
            if ($rs != false) {
                return $rs;
            }

            $model = new BargainModel();
//            if (isset($params['searchName'])) {
//                if ($params['searchName'] != "") {
//                    $where['shop_goods.name'] = ['like', "{$params['searchName']}"];
//                }
//            }
            $params['shop_bargain_record.key'] = $params['key'];
            unset($params['key']);
            $params['shop_bargain_record.merchant_id'] = yii::$app->session['uid'];
            $params['field'] = "shop_bargain_record.*,shop_goods.name,shop_goods.pic_urls";
            $params['join'][] = ['left join', 'shop_goods', 'shop_goods.id = shop_bargain_record.goods_id'];
            $array = $model->do_select($params);

            if($array['status'] == 200){
                foreach ($array['data'] as $k=>$v){
                    $array['data'][$k]['bargain_rule'] = json_decode($v['bargain_rule'],true);
                    $array['data'][$k]['format_bargain_start_time'] = date("Y-m-d H:i:s",$v['bargain_start_time']);
                    $array['data'][$k]['format_bargain_end_time'] = date("Y-m-d H:i:s",$v['bargain_end_time']);
                }
            }
            return $array;
        } else {
            return result(500, "请求方式错误");
        }
    }

    public function actionUpdate($id){
        if (yii::$app->request->isPut) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->bodyParams; //获取body传参
            //设置类目 参数
            $must = ['key', 'bargain_poster', 'bargain_rotation'];
            $rs = $this->checkInput($must, $params);
            if ($rs != false) {
                return $rs;
            }
            $model = new AppAccessModel();
            $params['id'] = $id;
            $params['`key`'] = $params['key'];
            unset($params['key']);
            $array = $model->update($params);
            if ($array['status'] == 200){
                //添加操作记录
                $operationRecordModel = new OperationRecordModel();
                $operationRecordData['key'] = $params['`key`'];
                $operationRecordData['merchant_id'] = yii::$app->session['uid'];
                $operationRecordData['operation_type'] = '更新';
                $operationRecordData['operation_id'] = $id;
                $operationRecordData['module_name'] = '砍价';
                $operationRecordModel->do_add($operationRecordData);
            }
            return $array;
        } else {
            return result(500, "请求方式错误");
        }
    }
}